<?php
/**
 * *********************************************************************************************************************
 * 当前服务器环境
 **********************************************************************************************************************
 */
namespace soa\check;

use artisan\config;

class EnvCheck{

    /**
     * 所有服务器名字
     * @var array
     */
    protected static $allEnvs = [
        //以下为81服务器
        'calhost.localdomai' => 'ENV_81',


        //以下为onlineTest服务器
        'bp1fu0fz7wj4bf21dp0v' => 'ENV_ONLINE_TEST', //Online_test_redis
        'bp119x3mzvro43druuf2' => 'ENV_ONLINE_TEST', //Online_test


        //以下为线上VPC服务器组
        'bp13x2885fz5uyht00b8' => 'ENV_ONLINE', //cloud_print_2
        'bp1c44dbw6pz6xiax6eu' => 'ENV_ONLINE', //open_1
        'bp17j8t9foad8jmx33yq' => 'ENV_ONLINE', //cloud_print_1
        'bp14pqjebfvelq4ay6hy' => 'ENV_ONLINE', //Crawler_6
        'bp14pqjebfvelq4ay6i0' => 'ENV_ONLINE', //Crawler_5
        'bp14pqjebfvelq4ay6hz' => 'ENV_ONLINE', //Crawler_4
        'bp10zn87r67atjyvty8c' => 'ENV_ONLINE', //Crawler_agent
        'bp1283ts0vfkpk53qrbb' => 'ENV_ONLINE', //Crawler_3
        'bp1283ts0vfkpk53qrba' => 'ENV_ONLINE', //Crawler_2
        'bp1ffc555fo84t3nt0kk' => 'ENV_ONLINE', //Crawler_1
        'bp197n2mmuyya5f6eq6v' => 'ENV_ONLINE', //pda_1
        'bp17f7xayish9rkrgsps' => 'ENV_ONLINE', //Redis_log
        'bp11xnscr5ek3m50nvmx' => 'ENV_ONLINE', //Redis_address
        'bp1147skj2ic6p31yho5' => 'ENV_ONLINE', //app_5
        'bp1i5bedzoazj97qvfil' => 'ENV_ONLINE', //app_4
        'bp17qovdcua9c2zxrxza' => 'ENV_ONLINE', //Address-2
        'bp17qovdcua9c2zxrxzb' => 'ENV_ONLINE', //Address-1
        'bp11lfls81zdjn93lv1k' => 'ENV_ONLINE', //Redis_message
        'bp1j3wem2kyfm4fdn7rh' => 'ENV_ONLINE', //Redis_pda
        'bp1j3wem2kyfm4fdn7rg' => 'ENV_ONLINE', //Redis-2
        'bp1j3wem2kyfm4fdn7ri' => 'ENV_ONLINE', //Redis-1
        'bp19ct64q02houipd949' => 'ENV_ONLINE', //log
        'bp11lfls81zdj9fvth5z' => 'ENV_ONLINE', //pda_2
        'bp141df2mogmodr4n71z' => 'ENV_ONLINE', //gitlab
        'bp1hfhto8e2uly3aixfh' => 'ENV_ONLINE', //web-1
        'bp1hfhto8e2uly3aixfi' => 'ENV_ONLINE', //web-2
        'bp12883z03k34x16bvlf' => 'ENV_ONLINE', //app-1
        'bp12883z03k34x16bvlh' => 'ENV_ONLINE', //app-2
        'bp12883z03k34x16bvle' => 'ENV_ONLINE', //app-3
        'bp12883z03k34x16bvld' => 'ENV_ONLINE', //CORE-1
        'bp12883z03k34x16bvla' => 'ENV_ONLINE', //CORE-2
        'bp12883z03k34x16bvli' => 'ENV_ONLINE', //CORE-3
        'bp12883z03k34x16bvlb' => 'ENV_ONLINE', //Express-1
        'bp12883z03k34x16bvlc' => 'ENV_ONLINE', //Express-2
        'bp12883z03k34x16bvlg' => 'ENV_ONLINE', //Express-3

        'bp1isotv99f3739rt1si' => 'ENV_ONLINE', //app_6
        'bp1isotv99f3739rt1sh' => 'ENV_ONLINE', //app_7
        'bp1isotv99f3739rt1sg' => 'ENV_ONLINE', //app_8
 

        'bp1exyy60ycylbumwnvh' => 'ENV_ONLINE', //core_5
        'bp1exyy60ycylbumwnvg' => 'ENV_ONLINE', //core_4
        'bp128lyt2y9rliqz18vl' => 'ENV_ONLINE', //express_4
        'bp128lyt2y9rliqz18vm' => 'ENV_ONLINE', //express_5
        'bp12ct08gsloot5w1dgr' => 'ENV_ONLINE', //pda_3
        'bp19adyup5hebacymwjc' => 'ENV_ONLINE', //web_3



        'bp13f16huohb25wf252p' => 'ENV_ONLINE', //接口请求分发
        'bp10ss8dnwqzon0ftpao' => 'ENV_ONLINE', //接口请求分发

        'bp1a7wyzv7b4w3clv5nn' => 'ENV_ONLINE', //docker_3
        'bp1hvzhawp93ndo7h9an' => 'ENV_ONLINE', //docker_2
    ];


    /**
     * 判断是否是81服务器，是81返回true；否则返回false
     * @return bool
     */
    public static function isEnv81(){
        return self::getCurrentEnv()=='ENV_81'?true:false;
    }

    /**
     * 判断是否是onlineTest服务器，是onlineTest返回true；否则返回false
     * @return bool
     */
    public static function isEnvOnlineTest(){
        return self::getCurrentEnv()=='ENV_ONLINE_TEST'?true:false;
    }

    /**
     * 判断是否是onlineECS服务器，是onlineECS返回true；否则返回false
     * @return bool
     */
    public static function isEnvOnline(){
        return self::getCurrentEnv()=='ENV_ONLINE'?true:false;
    }

    /**
     * 判断是否是onlineVPC服务器，是onlineVPC返回true；否则返回false
     * @return bool
     */
    public static function isEnvVPC(){
        return self::getCurrentEnv()=='ENV_VPC'?true:false;
    }


    /**
     * 判断是否是其它服务器(比如你本地环境)，是其它返回true；否则返回false
     * @return bool
     */
    public static function isEnvOther(){
        return self::getCurrentEnv()=='ENV_OTHER'?true:false;
    }


    /**
     * 根据当前服务器名获取当前服务器类型
     * （返回：'ENV_ONLINE'，'ENV_ONLINE_TEST'，'ENV_81'，'ENV_VPC'，'ENV_OTHER'）
     * @return mixed|string
     */
    public static function getCurrentEnv(){



        $hostName = substr(substr(getHostName(), 2), 0, -1);
        $curEnv= isset(self::$allEnvs[$hostName])?self::$allEnvs[$hostName]:'ENV_OTHER';
        //logDebug($curEnv.':'.debug_backtrace()[1]['function'],'env_cur',$hostName);
        return $curEnv;
    }

    /**
     * 判断某个url是否要做请求拦截
     * @return string|bool 不拦截的返回false
     */
    public static function skipTheUrl($url){
        //全局检测哪些接口不能走两套环境
        $skipEnvUrls=config::get('skip_url');
        $currentEnv=EnvCheck::getCurrentEnv();
        if(isset($skipEnvUrls[$currentEnv])&&is_array($skipEnvUrls[$currentEnv])){
            foreach($skipEnvUrls[$currentEnv] as $skipInfo){
                //如果当前环境下需要跳过该url
                if(false!==strstr($url,$skipInfo['url'])){
                    logDebug('当前环境:'.$currentEnv.' 拦截url:'.$url,'env_skip');//日志记录
                    //随机睡眠n微秒模拟该请求
                    usleep(rand($skipInfo['usleep'][0],$skipInfo['usleep'][1]));
                    //返回值取配置文件中的return字段
                    return $skipInfo['return'];
                }
            }
        }
        //logDebug('当前环境:'.$currentEnv.' 不拦截拦截url:'.$url,'env_noskip');//日志记录
        return false;
    }



    /******************下面的函数不允许再调，只为做临时兼容老函数名*******************************/
    //魔术方法__call
    public function __call($method,$arg){
        return self::__callStatic($method,$arg);
    }
    //魔术方法__callStatic
    public static function __callStatic($method,$arg){
        $oldFuncNames=[
            'is81Test'=>'isEnv81',
            'isOnlineTest'=>'isEnvOnlineTest',
            'isOnlineECS'=>'isEnvOnline',
            'isOnlineVPC'=>'isEnvVPC',
            'isOnlineEnv'=>'isEnvOnline',
        ];
        if(isset($oldFuncNames[$method])){
            return self::$oldFuncNames[$method]($arg);
        }
        logDebug('EnvCheck类函数名不存在:'.$method,'env_err');
        return 'EnvCheck类函数名不存在:'.$method;
    }
}